package com.sg.service.biz.app.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.common.util.RedisUtil;
import com.sg.dto.base.app.req.QueryBizAppCheckItemStatDetailReq;
import com.sg.dto.base.app.req.QueryEvaObjTargetCycleListReq;
import com.sg.dto.biz.app.req.BatchAddEvaTepCyclePlanReqDto;
import com.sg.dto.biz.app.req.RefreshEvaThemeCurrentBstepComReqDto;
import com.sg.dto.biz.app.req.RefreshTargetDashboardComReqDto;
import com.sg.dto.biz.app.res.BatchAddEvaTepCyclePlanRespDto;
import com.sg.dto.biz.app.res.RefreshEvaThemeCurrentBstepComRespDto;
import com.sg.dto.biz.app.res.RefreshTargetDashboardComRespDto;
import com.sg.entity.OmsBizAppStatusCheckItem;
import com.sg.entity.OmsEvaluationObjectTargetCycle;
import com.sg.entity.OmsTspePlanCycle;
import com.sg.service.base.app.MOmsBizAppStatusCheckItemService;
import com.sg.service.base.app.MOmsEvaluationObjectService;
import com.sg.service.base.app.MOmsEvaluationObjectTargetCycleService;
import com.sg.service.base.app.MOmsTspePlanCycleService;
import com.sg.service.biz.app.NullService;
import com.sg.service.biz.app.OffStateService;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class EvaWorkExecutionServiceImpl
        implements com.wicket.okrapp.biz.service.EvaWorkExecutionService {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private MOmsEvaluationObjectService mOmsEvaluationObjectService;
    @Resource
    private MOmsEvaluationObjectTargetCycleService mOmsEvaluationObjectTargetCycleService;
    @Resource
    private MOmsTspePlanCycleService mOmsTspePlanCycleService;
    @Resource
    private MOmsBizAppStatusCheckItemService mOmsBizAppStatusCheckItemService;
    @Resource
    private OffStateService offStateService;
    @Resource
    private NullService nullService;

    /**
     * D3-2批量增评价模板周期安排[2424]
     * gen by moon at 10/28/2022, 12:38:46 AM
     */
    @Trace(operationName = "D3-2批量增评价模板周期安排")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public BatchAddEvaTepCyclePlanRespDto batchAddEvaTepCyclePlan(BatchAddEvaTepCyclePlanReqDto reqDto) {


        //步骤0: 3-3-09查被评对象目标周期列表 - queryEvaObjTargetCycleList
        List<OmsEvaluationObjectTargetCycle> listOmsEvaluationObjectTargetCycle = new ArrayList<>();
        QueryEvaObjTargetCycleListReq queryEvaObjTargetCycleListReq = new QueryEvaObjTargetCycleListReq();
        queryEvaObjTargetCycleListReq.setEvaObjEntityId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID//sourceId:118992_1
        queryEvaObjTargetCycleListReq.setEvaObjTableTypeCode("OMS_FRAMEWORK_SPACE");//sourceId:118993_1
        queryEvaObjTargetCycleListReq.setEvaObjTypeCode("EVA_OBJECT");//sourceId:118994_1
        queryEvaObjTargetCycleListReq.setTreeTypeCode("PLAN_TREE");//sourceId:110075_1
        queryEvaObjTargetCycleListReq.setTargetCycleContentTypeCode("TARGET");//sourceId:110076_1
        queryEvaObjTargetCycleListReq.setIsSubCycle("TRUE");//sourceId:110084_1
        queryEvaObjTargetCycleListReq.setIsArchive("FALSE");//sourceId:110090_1
        queryEvaObjTargetCycleListReq.setArchivngType("NOT_ARCHIVE");//sourceId:178203_1
        if (reqDto != null) {
            queryEvaObjTargetCycleListReq.setTargetCycleContentId(reqDto.getTargetId());//SimpleFieldAssign//sourceId:110088_1
            queryEvaObjTargetCycleListReq.setTargetId(reqDto.getTargetId());//SimpleFieldAssign//sourceId:110093_1
        }

        /*3-3-09查被评对象目标周期列表（子周期）[2190]   */
        Assert.isNull(queryEvaObjTargetCycleListReq.getEvaObjEntityId(), "D3-2批量增评价模板周期安排-3-3-09查被评对象目标周期列表（子周期）-被评对象内容表主键ID不能为空", false);
        Assert.isNull(queryEvaObjTargetCycleListReq.getEvaObjTableTypeCode(), "D3-2批量增评价模板周期安排-3-3-09查被评对象目标周期列表（子周期）-被评对象内容表类型编码不能为空", false);
        Assert.isNull(queryEvaObjTargetCycleListReq.getEvaObjTypeCode(), "D3-2批量增评价模板周期安排-3-3-09查被评对象目标周期列表（子周期）-被评对象类型编码不能为空", false);
        Assert.isNull(queryEvaObjTargetCycleListReq.getTreeTypeCode(), "D3-2批量增评价模板周期安排-3-3-09查被评对象目标周期列表（子周期）-树类型编码不能为空", false);
        Assert.isNull(queryEvaObjTargetCycleListReq.getTargetCycleContentTypeCode(), "D3-2批量增评价模板周期安排-3-3-09查被评对象目标周期列表（子周期）-关联目标内容类型编码不能为空", false);
        Assert.isNull(queryEvaObjTargetCycleListReq.getTargetCycleContentId(), "D3-2批量增评价模板周期安排-3-3-09查被评对象目标周期列表（子周期）-关联目标内容ID不能为空", false);
        Assert.isNull(queryEvaObjTargetCycleListReq.getIsSubCycle(), "D3-2批量增评价模板周期安排-3-3-09查被评对象目标周期列表（子周期）-是否子周期不能为空", false);
        Assert.isNull(queryEvaObjTargetCycleListReq.getTargetId(), "D3-2批量增评价模板周期安排-3-3-09查被评对象目标周期列表（子周期）-冗余目标ID不能为空", false);
        Assert.isNull(queryEvaObjTargetCycleListReq.getIsArchive(), "D3-2批量增评价模板周期安排-3-3-09查被评对象目标周期列表（子周期）-是否存档不能为空", false);
        Assert.isNull(queryEvaObjTargetCycleListReq.getArchivngType(), "D3-2批量增评价模板周期安排-3-3-09查被评对象目标周期列表（子周期）-存档类型不能为空", false);
        listOmsEvaluationObjectTargetCycle = mOmsEvaluationObjectTargetCycleService.queryEvaObjTargetCycleList(queryEvaObjTargetCycleListReq);
        Assert.isTrue(listOmsEvaluationObjectTargetCycle == null || CollectionUtil.isEmpty(listOmsEvaluationObjectTargetCycle) || listOmsEvaluationObjectTargetCycle == null || CollectionUtil.isEmpty(listOmsEvaluationObjectTargetCycle) || listOmsEvaluationObjectTargetCycle.size() == 0, "找不到数据，系统异常", false);


//步骤1: 3-2-03批量增评价模板安排周期 - batchAddEvaTepPlanCycle
        boolean bOOLEAN;
        if (listOmsEvaluationObjectTargetCycle != null && !CollectionUtil.isEmpty(listOmsEvaluationObjectTargetCycle) && listOmsEvaluationObjectTargetCycle.size() > 0) {
            List<OmsTspePlanCycle> listOmsTspePlanCycle = new ArrayList<>();
            if (listOmsEvaluationObjectTargetCycle != null && !CollectionUtil.isEmpty(listOmsEvaluationObjectTargetCycle) && listOmsEvaluationObjectTargetCycle != null && !CollectionUtil.isEmpty(listOmsEvaluationObjectTargetCycle)) {
                listOmsTspePlanCycle = //objList-to-objLists
                        listOmsEvaluationObjectTargetCycle.stream().map(item -> {
                            OmsTspePlanCycle elm = new OmsTspePlanCycle();
                            elm.setEvaObjTargetCycleId(item.getEvaObjTargetCycleId());//SimpleFieldAssign//sourceId:59293_2
                            elm.setIsReqCycleRange("TRUE");//sourceId:59342_2
                            if (reqDto != null) {
                                elm.setEvaluationTemplateId(reqDto.getEvaluationTemplateId());//SimpleFieldAssign//sourceId:76110_2
                            }
                            return elm;
                        }).collect(Collectors.toList());//objList-to-objLists//sourceId:110071_1
            }

            /*3-2-03批量增评价模板安排周期[2357]   */

            bOOLEAN = mOmsTspePlanCycleService.batchAddEvaTepPlanCycle(listOmsTspePlanCycle);


        }

//步骤2: 3-11-04查业务应用校验项状态详情 - queryBizAppCheckItemStatDetail
        OmsBizAppStatusCheckItem omsBizAppStatusCheckItem = null;
        QueryBizAppCheckItemStatDetailReq queryBizAppCheckItemStatDetailReq = new QueryBizAppCheckItemStatDetailReq();
        queryBizAppCheckItemStatDetailReq.setObjectTypeCode("OMS_TARGET");//sourceId:110132_1
        queryBizAppCheckItemStatDetailReq.setCheckItemCode("CYCLE_ARRANGE_REQUEST_CHECK_ITEM");//CUSTOM_CONVENTION//sourceId:160344_1
        if (reqDto != null) {
            queryBizAppCheckItemStatDetailReq.setEntityId(reqDto.getTargetId());//SimpleFieldAssign//sourceId:110133_1
        }

        /*3-11-04查业务应用校验项状态（安排周期）[2661]   */
        Assert.isNull(queryBizAppCheckItemStatDetailReq.getEntityId(), "D3-2批量增评价模板周期安排-3-11-04查业务应用校验项状态（安排周期）-内容对象实例ID不能为空", false);
        Assert.isNull(queryBizAppCheckItemStatDetailReq.getObjectTypeCode(), "D3-2批量增评价模板周期安排-3-11-04查业务应用校验项状态（安排周期）-内容对象类型编码不能为空", false);
        Assert.isNull(queryBizAppCheckItemStatDetailReq.getCheckItemCode(), "D3-2批量增评价模板周期安排-3-11-04查业务应用校验项状态（安排周期）-检查项标识不能为空", false);
        omsBizAppStatusCheckItem = mOmsBizAppStatusCheckItemService.queryBizAppCheckItemStatDetail(queryBizAppCheckItemStatDetailReq);
        Assert.isTrue(omsBizAppStatusCheckItem == null || omsBizAppStatusCheckItem.getStatusCheckItemId() == null, "找不到数据，系统异常", false);


//步骤3: 3-11-04修改业务应用校验项状态 - updateBizAppCheckItemStat
        boolean bOOLEAN_1;
        if (omsBizAppStatusCheckItem != null) {
            OmsBizAppStatusCheckItem omsBizAppStatusCheckItem_2 = new OmsBizAppStatusCheckItem();
            omsBizAppStatusCheckItem_2.setIsComplete("TRUE");//sourceId:110129_1
            if (omsBizAppStatusCheckItem != null) {
                omsBizAppStatusCheckItem_2.setStatusCheckItemId(omsBizAppStatusCheckItem.getStatusCheckItemId());//SimpleFieldAssign//sourceId:110128_1
            }

            /*3-11-04修改业务应用校验项状态（安排周期）[2660]   */
            Assert.isNull(omsBizAppStatusCheckItem_2.getStatusCheckItemId(), "D3-2批量增评价模板周期安排-3-11-04修改业务应用校验项状态（安排周期）-状态校验项ID不能为空", false);
            Assert.isNull(omsBizAppStatusCheckItem_2.getIsComplete(), "D3-2批量增评价模板周期安排-3-11-04修改业务应用校验项状态（安排周期）-是否已完成不能为空", false);
            bOOLEAN_1 = mOmsBizAppStatusCheckItemService.updateBizAppCheckItemStat(omsBizAppStatusCheckItem_2);


        }

//步骤4: D3-13更新目标自身仪表盘(公共) - refreshTargetDashboardCom
        RefreshTargetDashboardComRespDto refreshTargetDashboardComRespDto = null;
        RefreshTargetDashboardComReqDto refreshTargetDashboardComReqDto = new RefreshTargetDashboardComReqDto();
        refreshTargetDashboardComReqDto.setCheckItemCode("CYCLE_ARRANGE_REQUEST_CHECK_ITEM");//CUSTOM_CONVENTION//sourceId:384005_1
        refreshTargetDashboardComReqDto.setActualValue(Long.valueOf(1));//CUSTOM_CONVENTION//sourceId:384007_1
        refreshTargetDashboardComReqDto.setTargetValue(Long.valueOf(1));//CUSTOM_CONVENTION//sourceId:384006_1
        if (reqDto != null) {
            refreshTargetDashboardComReqDto.setCheckObjectTypeId(reqDto.getTargetId());//SimpleFieldAssign//sourceId:384004_1
        }

        /*D3-13更新目标自身仪表盘(公共)[4963]   */
        Assert.isNull(refreshTargetDashboardComReqDto.getCheckObjectTypeId(), "D3-2批量增评价模板周期安排-D3-13更新目标自身仪表盘(公共)-检查内容对象ID 不能为空", false);
        Assert.isNull(refreshTargetDashboardComReqDto.getCheckItemCode(), "D3-2批量增评价模板周期安排-D3-13更新目标自身仪表盘(公共)-检验项标识不能为空", false);
        Assert.isNull(refreshTargetDashboardComReqDto.getActualValue(), "D3-2批量增评价模板周期安排-D3-13更新目标自身仪表盘(公共)-实际数值不能为空", false);
        Assert.isNull(refreshTargetDashboardComReqDto.getTargetValue(), "D3-2批量增评价模板周期安排-D3-13更新目标自身仪表盘(公共)-目标数值不能为空", false);
        refreshTargetDashboardComRespDto = nullService.refreshTargetDashboardCom(refreshTargetDashboardComReqDto);


//步骤5: D3-3更新评价主题当前步骤(公共) - refreshEvaThemeCurrentBstepCom
        RefreshEvaThemeCurrentBstepComRespDto refreshEvaThemeCurrentBstepComRespDto = null;
        RefreshEvaThemeCurrentBstepComReqDto refreshEvaThemeCurrentBstepComReqDto = new RefreshEvaThemeCurrentBstepComReqDto();
        refreshEvaThemeCurrentBstepComReqDto.setBizSceneCode("ADD_EVA_TEMP");//CUSTOM_CONVENTION//sourceId:406143_1
        refreshEvaThemeCurrentBstepComReqDto.setOrderNumber(Long.valueOf(4));//CUSTOM_CONVENTION//sourceId:346411_1
        if (reqDto != null) {
            refreshEvaThemeCurrentBstepComReqDto.setEvaluationTemplateId(reqDto.getEvaluationTemplateId());//SimpleFieldAssign//sourceId:346409_1
        }

        /*D3-3更新评价主题当前步骤(公共)[4615]   */
        Assert.isNull(refreshEvaThemeCurrentBstepComReqDto.getEvaluationTemplateId(), "D3-2批量增评价模板周期安排-D3-3更新评价主题当前步骤(公共)-评价模板ID不能为空", false);
        Assert.isNull(refreshEvaThemeCurrentBstepComReqDto.getBizSceneCode(), "D3-2批量增评价模板周期安排-D3-3更新评价主题当前步骤(公共)-业务场景标识不能为空", false);
        Assert.isNull(refreshEvaThemeCurrentBstepComReqDto.getOrderNumber(), "D3-2批量增评价模板周期安排-D3-3更新评价主题当前步骤(公共)-排序不能为空", false);
        refreshEvaThemeCurrentBstepComRespDto = offStateService.refreshEvaThemeCurrentBstepCom(refreshEvaThemeCurrentBstepComReqDto);


        BatchAddEvaTepCyclePlanRespDto retData = new BatchAddEvaTepCyclePlanRespDto();


        return retData;
    }
    //
}
